package com.dg.river.service;

import android.content.Context;

import com.dg.river.contant.CommonCode;
import com.dg.river.core.util.L;
import com.dg.river.module.common.bean.EbMessage;
import com.dg.river.service.bean.GeTuiMessageBean;
import com.dg.river.storage.AppSettingManager;
import com.google.gson.Gson;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by jrj
 * Description :
 */
public class GeTuiService extends GTIntentService {

    @Override
    public void onReceiveServicePid(Context context, int pid) {

    }

    // 处理透传消息
    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        byte[] payload = msg.getPayload();
        if (payload != null) {
            String data = new String(payload);
            GeTuiMessageBean geTuiMessageBean = new Gson().fromJson(data, GeTuiMessageBean.class);
            String title = geTuiMessageBean.getTitle();
            L.d("个推=====收到透传消息：" + title);
        }
    }

    // 接收 cid
    @Override
    public void onReceiveClientId(Context context, String clientId) {
        L.d("个推=====onReceiveClientId -> " + "clientId = " + clientId);
        // 传递个推Id给启动页
        EventBus.getDefault().post(new EbMessage(CommonCode.EventBus.SEND_GT_ID, clientId));
        // 保存个推Id
        AppSettingManager.setPushGtClientId(context, clientId);
    }

    // cid 离线上线通知
    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
        L.d("个推=====" + (online ? "上线" : "下线"));
    }

    // 各种事件处理回执
    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
        L.d("个推=====事件处理回执：" + cmdMessage.getAction());
    }

    // 通知到达，只有个推通道下发的通知会回调此方法
    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage msg) {
        L.d("个推=====收到个推通知");
        // 刷新首页未读消息数
        EventBus.getDefault().post(new EbMessage(CommonCode.EventBus.REFRESH_MAIN_UNREAD_COUNT, null));
        // 刷新消息列表
        EventBus.getDefault().post(new EbMessage(CommonCode.EventBus.REFRESH_MESSAGE_MAIN_LIST, null));
        // 刷新首页快捷通知
        EventBus.getDefault().post(new EbMessage(CommonCode.EventBus.REFRESH_MAIN_QUICK_MESSAGE, null));
        // 刷新报港审核页面、进行中订单页面
        EventBus.getDefault().post(new EbMessage(CommonCode.EventBus.REFRESH_REPORT_REMOTE_INFO, null));
        // 刷新订单列表
        EventBus.getDefault().post(new EbMessage(CommonCode.EventBus.REFRESH_ORDER_LIST, null));
        // 刷新补费订单列表
        EventBus.getDefault().post(new EbMessage(CommonCode.EventBus.REFRESH_SUPPLEMENTARY_ORDER_LIST, null));
    }

    // 通知点击，只有个推通道下发的通知会回调此方法
    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage message) {
        L.d("个推=====通知点击");
    }
}
